﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.NhaDat;
using SouthernLand.Common.BOL.User;
using SouthernLand.Common.Untilities;

namespace SouthernLand.WebMaster.Controls
{
    public partial class Dangtin : UserControl
    {
        private int RaovatID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            RaovatID = Globals.GetIntFromQueryString("RaovatID");

            if (!Page.IsPostBack)
            {
                Load_ddlQuanHuyen();
                dlDonVT.SelectedIndex = 1; // uu tien cho Trieu VND

                if (RaovatID > 0) // khi chinh sua
                {
                    Load_RaovatByID(RaovatID);
                    imgSave.ImageUrl = "~/Images/btSuaTinRao.gif";
                }
                else if (Globals.CurrentUserID.Length > 10) // neu da dang nhap va muon dang tin
                {
                    LoadProfile(); // load thong tin lien he cu
                }
            }
        }

        private void Load_RaovatByID(int RaovatID)
        {
            Raovat obj = Raovat.GetRaovatByID(RaovatID);
            if (obj != null)
            {
                dlLoaitin.SelectedValue = obj.Loaitin.ToString();
                dlLoaiBDS.SelectedValue = obj.CateID.ToString();
                dlTinhthanh.SelectedValue = obj.Tinhthanh.ToString();
                List<QuanHuyen> objDis = QuanHuyen.GetQuanHuyenByTinhThanhID(ConvertUtility.ToInt32(obj.Tinhthanh));
                dlQuanHuyen.DataSource = objDis;
                dlQuanHuyen.DataValueField = "QuanHuyenID";
                dlQuanHuyen.DataTextField = "TenQuanHuyen";
                dlQuanHuyen.DataBind();
                dlQuanHuyen.SelectedValue = obj.Quanhuyen.ToString();
                txtDiachiNha.Text = obj.DiachiNha.Trim();
                txtDientichDat.Text = obj.DientichDat.ToString();
                txtDientichXD.Text = obj.DientichXD.ToString();

                txtSolau.Text = obj.Solau.ToString().Replace("0", " ");
                txtPhongNgu.Text = obj.PhongNgu.ToString().Replace("0", " ");
                txtPhongVS.Text = obj.PhongVS.ToString().Replace("0", " ");

                if (obj.Vitri.ToLower().Trim() == "trong hẻm")
                    rdoTronghem.Checked = true;
                else
                    rdoDuonglon.Checked = true;

                dlTinhTrangPL.SelectedValue = obj.TinhtrangPL.ToString();
                dlHuongNha.SelectedValue = obj.HuongNha.ToString();
                txtGia.Text = Formatter.FormatMoney(obj.Gia);
                dlDonVT.SelectedValue = obj.DonVT.ToString();
                dlGiaTren.SelectedValue = obj.GiaTren;
                txtTieude.Text = obj.Tieude;
                txtMota.Text = obj.Mota.Replace("<br>", "/n");
                hHinh1.Value = obj.Hinh1.Trim();

                if (obj.DisplayOrder == 100)
                    chkVip.Checked = true;

                txtHoten.Text = obj.Hoten;
                txtDiachi.Text = obj.Diachi;
                txtEmail.Text = obj.Email;
                txtDienthoai.Text = obj.Dienthoai;
                txtDidong.Text = obj.Didong;
            }
        }

        protected void ImgSave_Click(object sender, ImageClickEventArgs e)
        {
            string strUpload = Hinh1_Upload();

            if (strUpload == "error1")
            {
                PanelMsg.Visible = true;
                lblMsg.Text = "Xin lỗi! File hình ảnh bạn chọn không hợp lệ.";
            }
            else
            {
                var obj = new Raovat();
                obj.RaovatID = RaovatID;
                obj.UserID = Globals.CurrentUserID;
                obj.CateID = ConvertUtility.ToInt32(dlLoaiBDS.SelectedItem.Value);
                obj.Loaitin = ConvertUtility.ToInt32(dlLoaitin.SelectedItem.Value);
                obj.Tinhthanh = ConvertUtility.ToInt32(dlTinhthanh.SelectedItem.Value);
                obj.Quanhuyen = ConvertUtility.ToInt32(dlQuanHuyen.SelectedItem.Value);
                obj.DiachiNha = txtDiachiNha.Text.Trim();
                if (txtDientichDat.Text.IndexOf(',') != -1)
                {
                    txtDientichDat.Text = txtDientichDat.Text.Replace(',', '.');
                }
                obj.DientichDat = !string.IsNullOrEmpty(txtDientichDat.Text)
                                      ? ConvertUtility.ToDouble(txtDientichDat.Text)
                                      : 0;
                if(txtDientichXD.Text.IndexOf(',')!=-1)
                {
                    txtDientichXD.Text = txtDientichXD.Text.Replace(',', '.');
                }
                obj.DientichXD = !string.IsNullOrEmpty(txtDientichXD.Text.Trim())
                                     ? ConvertUtility.ToDouble(txtDientichXD.Text)
                                     : 0;
                obj.Solau = !string.IsNullOrEmpty(txtSolau.Text.Trim()) ? ConvertUtility.ToInt32(txtSolau.Text) : 0;
                obj.PhongNgu = !string.IsNullOrEmpty(txtPhongNgu.Text.Trim())
                                   ? ConvertUtility.ToInt32(txtPhongNgu.Text)
                                   : 0;
                obj.PhongVS = !string.IsNullOrEmpty(txtPhongVS.Text.Trim())
                                  ? ConvertUtility.ToInt32(txtPhongVS.Text)
                                  : 0;

                obj.TinhtrangPL = ConvertUtility.ToInt32(dlTinhTrangPL.SelectedItem.Value) > 0
                                      ? ConvertUtility.ToInt32(dlTinhTrangPL.SelectedItem.Value)
                                      : 0;

                obj.HuongNha = ConvertUtility.ToInt32(dlHuongNha.SelectedItem.Value) > 0
                                   ? ConvertUtility.ToInt32(dlHuongNha.SelectedItem.Value)
                                   : 0;

                obj.Vitri = rdoDuonglon.Checked ? rdoDuonglon.Text : rdoTronghem.Text;

                obj.DuongtruocNha = 0;
                if (txtGia.Text.IndexOf(',')!=-1)
                {
                    txtGia.Text = txtGia.Text.Replace(',', '.');
                }
                obj.Gia = !string.IsNullOrEmpty(txtGia.Text.Trim()) ? Convert.ToDecimal(txtGia.Text) : 0;

                obj.DonVT = ConvertUtility.ToInt32(dlDonVT.SelectedItem.Value);
                obj.GiaTren = dlGiaTren.SelectedItem.Value;
                obj.Tieude = txtTieude.Text.Trim();
                obj.Mota = txtMota.Text.Replace(Environment.NewLine, "<br>");

                obj.Hinh1 = strUpload;
                obj.Hoten = txtHoten.Text.Trim();
                obj.Diachi = txtDiachi.Text.Trim();
                obj.Email = txtEmail.Text.Trim();
                obj.Dienthoai = txtDienthoai.Text.Trim();
                obj.Didong = txtDidong.Text.Trim();

                obj.AddedDate = DateTime.Now;
                obj.ExpireDate = DateTime.Now.AddDays(ConvertUtility.ToInt32(dlThoigianDangtin.SelectedItem.Value));
                obj.IsActive = true;
                obj.DisplayOrder = chkVip.Checked ? 100 : 1;
                obj.TotalViews = 1;

                if (Globals.GetIntFromQueryString("RaovatID") > 0)
                {
                    Raovat.Update(obj);
                }
                else
                {
                    RaovatID = Raovat.Insert(obj);
                }

                if (chkVip.Checked)
                    Response.Redirect(Globals.ApplicationPath + "WebMaster/Raovat/Default.aspx?Trangthai=" +
                                      eTrangthai.TinVip);
                else
                    Response.Redirect(Globals.ApplicationPath + "WebMaster/Raovat/Default.aspx?Trangthai=" +
                                      eTrangthai.HieuLuc);
            }
        }

        protected void dlTinhThanh_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_ddlQuanHuyen();
        }

        protected void LoadProfile()
        {
            User obj = User.GetUserByID(Globals.CurrentUserID);
            if (obj != null)
            {
                txtHoten.Text = obj.LastName + " " + obj.FirstName;
                txtEmail.Text = obj.Email;
                txtDiachi.Text = obj.Address;
                txtDienthoai.Text = obj.Phone;
                txtDidong.Text = obj.Mobile;
            }
        }

        private void Load_ddlQuanHuyen()
        {
            List<QuanHuyen> obj =
                QuanHuyen.GetQuanHuyenByTinhThanhID(ConvertUtility.ToInt32(dlTinhthanh.SelectedItem.Value));
            dlQuanHuyen.DataSource = obj;
            dlQuanHuyen.DataValueField = "QuanHuyenID";
            dlQuanHuyen.DataTextField = "TenQuanHuyen";
            dlQuanHuyen.DataBind();
            dlQuanHuyen.Items.Insert(0, new ListItem(ResourceManager.GetString("PleaseSelect"), ""));
        }

        #region Upload_Hinh --------------------------------------

        protected string Hinh1_Upload()
        {
            string strFileName = "";

            // kiem tra xem user co chon hinh  ko
            if (Hinh1.HasFile)
            {
                if (Globals.CheckExtensionImage(Hinh1.FileName))
                {
                    // lay ten file de khong bi trung file + phan mo rong cua file can upload
                    strFileName = Common.Common.GetFileName() + Path.GetExtension(Hinh1.FileName);

                    //duong dan tao save file Large
                    string strFilePath = Request.PhysicalApplicationPath + "UserUpload/NhaDat/" + strFileName;
                    Hinh1.SaveAs(strFilePath);
                }
                else
                {
                    PanelMsg.Visible = true;
                    lblMsg.Text = "Hình thứ 1 bạn chọn không hợp lệ";
                    strFileName = "error1";
                }
            }
            else
                strFileName = hHinh1.Value.Trim();

            return strFileName;
        }

        #endregion Upload_Hinh
    }
}